﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;

namespace FonShare.Classes.FTP
{
    /// <summary>
    /// FXPTransfer class to send command to FTP servers to send and receive files.
    /// </summary>
    /// <author>spesme</author>
    /// <since>26/06/2009</since>
    public class FS_FXPTransfer
    {
        /// <summary>
        /// Inits a FTP transfer between 2 FTP Servers.
        /// </summary>
        /// <returns>A Message informing about the error or OK.</returns>
        /// <author>spesme</author>
        /// <since>26/06/2009</since>
        public static string InitTransfer()
        {
            string result = "OK!";
            string myFile = "US08-67Acpt.pdf";
            //string myFile = "npp.5.1.1.Installer.exe";
            FS_FTPFactory ftpServer1 = new FS_FTPFactory("10.10.70.34", ".", "sebastien", "twist");
            //FS_FTPFactory ftpServer1 = new FS_FTPFactory("10.10.60.65", ".", "sebastien", "twist");
            //FS_FTPFactory ftpServer2 = new FS_FTPFactory("10.10.70.34", ".", "sebastien", "twist");
            FS_FTPFactory ftpServer2 = new FS_FTPFactory("10.10.60.65", ".", "sebastien", "twist");
            try
            {
                ftpServer1.login();
            }
            catch (Exception ex)
            {
                return "login1 failed!";
            }
            try
            {
                ftpServer2.login();
            }
            catch (Exception ex)
            {
                return "login2 failed!";
            }
            try
            {
                ftpServer1.enterPassiveMode();
            }
            catch (Exception ex)
            {
                return "Passivate1 failed!";
            }
            try
            {
                ftpServer2.enterPassiveMode();
            }
            catch (Exception ex)
            {
                return "Passivate2 failed!";
            }
            string dataport = ftpServer2.getDataPort();
            try
            {
                ftpServer1.openDataPort(dataport);
            }
            catch (Exception ex)
            {
                return "Server1 - Open DataPort failed!";
            }
            try
            {
                ftpServer1.setBinaryMode(true);
            }
            catch (Exception ex)
            {
                return "Server1 - SetBinaryMode failed!";
            }
            try
            {
                ftpServer2.setBinaryMode(true);
            }
            catch (Exception ex)
            {
                return "Server2 - SetBinaryMode failed!";
            }
            try
            {
                ftpServer2.storeFile(myFile);
            }
            catch (Exception ex)
            {
                return "Server1 - Store file failed!";
            }
            try
            {
                ftpServer1.retrieveFile(myFile);
            }
            catch (Exception ex)
            {
                return "Server1 - Retrieve file failed!";
            }

            Thread.Sleep(20000);
            try
            {
                ftpServer1.close();
            }
            catch (Exception ex)
            {
                return "Server1 - Close failed!";
            }
            try
            {
                ftpServer2.close();
            }
            catch (Exception ex)
            {
                return "Server2 - Close failed!";
            }
            return result + " - " + dataport;
        }
    }
}
